package com.miquan.minotehelper.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

public class x {
    public static boolean DEBUG = false;

    private static void log(String msg) {
        Log.e("mq", "xxx  " + msg);
    }

    public static void xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(Context ctx, String msgToToast) {
        Toast.makeText(ctx, msgToToast, Toast.LENGTH_SHORT).show();
    }

    //基本类型
    public static void xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(String msg) {
        log("" + msg);
    }

    public static void xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(int msg) {
        log("" + msg);
    }

    public static void xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(float msg) {
        log("" + msg);
    }

    public static void xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(double msg) {
        log("" + msg);
    }

    public static void xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(boolean msg) {
        log("" + msg);
    }

    public static void xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(int width, int height) {
        log("" + width + "*" + height);
    }

    public static void xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(float width, float height) {
        log("" + width + "*" + height);
    }

    public static void xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(String tag, int msg) {
        log(tag + " >> " + msg);
    }

    public static void xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(String tag, float msg) {
        log(tag + " >> " + msg);
    }

    public static void xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(String tag, String msg) {
        log(tag + " >> " + msg);
    }

    /**
     * 打印一个对象
     *
     * @param obj
     */
    public static void xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(Object obj) {
        //list
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                log(o.toString());
            }
            return;
        }

        log(obj.toString());
    }

    //////////////////////////////////// 数据库测试开始 /////////////////////////////////////
    private final static String DIRECTOR_NAME = ".abc";
    public static BroadcastReceiver dbReceiver;

    /**
     * 打开广播接收。在第一个Activity打开吧，记得在onDestory关闭。
     * 需要权限：WRITE_EXTERNAL_STORAGE
     */
    public static void startDBReceiver(Context context) {
        if (DEBUG) {
            if (dbReceiver == null) {
                dbReceiver = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        x.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx("--- copyDB2SDCard start ---");
                        writeToSDCard(context);
                        x.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx("--- copyDB2SDCard end ---");
                        context.sendBroadcast(new Intent("com.miquan.BROADCAST_CATCH_DB_DOWN"));
                    }
                };
            }
            IntentFilter filter = new IntentFilter();
            filter.addAction("com.miquan.BROADCAST_CATCH_DB");
            context.registerReceiver(dbReceiver, filter);
        }
    }

    public static void stopDBReceiver(Context context) {
        if (DEBUG) {
            if (dbReceiver != null) {
                context.unregisterReceiver(dbReceiver);
            }
        }
    }

    /**
     * @param name 实体类名
     */
    private static void writeTable(Context context, String name) {
        BufferedWriter bw = null;
        try {
            File directory = new File(Environment.getExternalStorageDirectory(), DIRECTOR_NAME);
            if (!directory.exists()) directory.mkdirs();

            File file = new File(directory, name);
            if (!file.exists()) file.createNewFile();

            bw = new BufferedWriter(new FileWriter(file));

            // 反射调用对应的方法把数据写入到sdcard中
            Method method = x.class.getDeclaredMethod(name, Context.class, BufferedWriter.class);
            method.invoke(null, context, bw);

            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static String getLine(String... strs) {
        String line = "";
        for (String s : strs) {
            s = s.replace("\n", ",");
            s = s.replace("\r", ",");
            s = s.replace("\n\r", ",");
            s = s.replace("\r\n", ",");
            s = s.replace("`'", ",");
            line += s + "`'";
        }
        return line.substring(0, line.length() - 2);
    }

    private static void writeLine(BufferedWriter bw, String line) throws IOException {
        bw.write(line + "\n");
    }
    //////////////////////////////////// 数据库测试结束 /////////////////////////////////////

    // --- TAG DB TEST START ---

    public static void writeToSDCard(Context context) {
    }
    // --- TAG DB TEST END ---
}










